Typing Coroutines
نویسندگان
چکیده
A coroutine is a programming construct between function and thread. It can be invoked like a function, but before it returns a value (if ever) it may suspend itself arbitrarily often to return intermediate results and then be resumed with new inputs. Unlike with preemptive threading, a coroutine does not run concurrently with the rest of the program, but rather takes control until it voluntarily suspends to either return control to its caller or to pass control to another coroutine. Coroutines are close to cooperative threading, but they add value because they are capable of passing values into and out of the coroutine and they permit explicit switching of control. The main uses of coroutines are the implementation of compositions of state machines and the implementation of generators. The latter use has lead to renewed interest in coroutines and to their inclusion in mainstream languages like C#, albeit in restricted form as generators. Despite the renewed interest in the programming construct per se, the typing aspects of coroutines have not received much attention. Indeed, the supporting languages are either untyped (e.g., Lua, Scheme, Python), the typing for coroutines is trivalized, or coroutines are restricted so that a very simple typing is sufficient. For instance, in Modula-2, coroutines are created from parameterless procedures so that all communication between coroutines must take place through global variables. Also, for describing generators, a simple function type seems sufficient. We propose a static type system for coroutines where coroutines are first-class values, coroutine operations can be performed within nested function calls, and both asymmetric (generator-style) and symmetric (task-switching-style) coroutine operators are available. Moreover, we permit passing arguments to a coroutine at each start and resume operations and we permit returning results on each suspend and on termination of the coroutine (and we distinguish these two events). Our type system is based on the simply-typed lambda calculus extended with effects that describe the way the coroutine operations are used. We present a small-step operational semantics for the language and prove type soundness.
منابع مشابه
Christian - Albrechts - Universität Kiel
1 A coroutine is a programming construct between function and thread. It behaves like a function that can suspend itself arbitrarily often to yield intermediate results and to get new inputs before returning a result. This facility makes coroutines suitable for implementing generator abstractions. Languages that support coroutines are often untyped or they use trivial types for coroutines. This...
متن کاملCoroutines in Lua
After a period of oblivion, a renewal of interest in coroutines is being observed. However, most current implementations of coroutine mechanisms are restricted, and motivated by particular uses. The convenience of providing true coroutines as a general control abstraction is disregarded. This paper presents and discusses the coroutine facilities provided by the language Lua, a full implementati...
متن کاملDeriving Type Systems and Implementations for Coroutines
Starting from reduction semantics for several styles of coroutines from the literature, we apply Danvy’s method to obtain equivalent functional implementations (definitional interpreters) for them. By applying existing type systems for programs with continuations, we obtain sound type systems for coroutines through the translation. The resulting type systems are similar to earlier hand-crafted ...
متن کاملA verified abstract machine for functional coroutines
Functional coroutines are a restricted form of control mechanism, where each coroutine is represented with both a continuation and an environment. This restriction was originally obtained by considering a constructive version of Parigot’s classical natural deduction which is sound and complete for the Constant Domain logic. In this article, we present a refinement of de Groote’s abstract machin...
متن کاملA Low-Cost Implementation of Coroutines for C
We identify a set of primitive operations supporting coroutines, and demonstrate their usefulness. We then address their implementation in C according to a set of criteria aimed at maintaining simplicity, and achieve a satisfactory compromise between it and effectiveness. Our package for the PDP-II under UNIXt allows users of coroutines in C programs to gain access to the primitives via an incl...
متن کامل